package com.aegean.CommercialHotelBookSystem.user.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.aegean.CommercialHotelBookSystem.factory.BusinessFactory;
import com.aegean.CommercialHotelBookSystem.factory.MyTools;
import com.aegean.CommercialHotelBookSystem.model.Book;
import com.aegean.CommercialHotelBookSystem.model.Customer;
import com.aegean.CommercialHotelBookSystem.user.business.IUserBusiness;

/**
 * Servlet implementation class BookServiceTide
 */
public class BookServiceTide extends HttpServlet {
	List<Book> books;
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BookServiceTide() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String hotel_Name;
		String book_Date;
		if (request.getParameter("hotel_Name") == null) {
			hotel_Name = null;
		} else {
			hotel_Name = MyTools.toGB2312(request.getParameter("hotel_Name"));
		}
		if (request.getParameter("book_Date") == null) {
			book_Date = null;
		} else {
			book_Date = request.getParameter("book_Date");
		}
		HttpSession session = request.getSession();
		Customer customer = (Customer) session.getAttribute("Login_User");
		IUserBusiness business = BusinessFactory.createUserBusiness();
		books = business.searchBooks(customer, hotel_Name, book_Date);
		if (books == null||books.isEmpty()) {
			books = new ArrayList<Book>();
		}
		int size = 15;
		int pagecount = (books.size() + size - 1) / size;
		session.setAttribute("User_Books", books);
		session.setAttribute("User_BooksPage", pagecount);
		request.getRequestDispatcher("User_book.action").forward(request,
				response);
	}

}
